//
//  SZLAccountViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/8/5.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLAccountViewController.h"
#import "MyAccountHeader.h"
#import "MyCountNetwork.h"
#import "SZLAccountHelper.h"
#import "MBProgressHUD+MJ.h"
#import "SZLDrawListViewController.h"
#import "BillDetailCell.h"
#import <MJRefresh.h>

@interface SZLAccountViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,weak) UITableView *sourceTab;
@property (nonatomic,weak) MyAccountHeader *header;
@property (nonatomic,strong) MyCountModel *model;

@property (nonatomic,assign) NSInteger curPage;
@property (nonatomic,strong) NSMutableArray *sources;

@end

@implementation SZLAccountViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的账户";
    [self setTableView];
    self.curPage = 1;
    [self getMyCount];
    [self getWithDrawLsit];
}

- (void)setTableView
{
    MyAccountHeader *header = [MyAccountHeader accountHeader];
    header.frame = CGRectMake(0, 0, ScreenW, 140);
    __WEAK
    header.getCashBlock = ^(){
        SZLDrawListViewController *drawVc = [[SZLDrawListViewController alloc]init];
        drawVc.model = weakSelf.model;
        [self.navigationController pushViewController:drawVc animated:YES];
    };
    self.header = header;
    [self.view addSubview:header];
    
    UITableView *sourceTab = [[UITableView alloc]initWithFrame:CGRectMake(0,140, ScreenW, ScreenH-64-140)];
    sourceTab.dataSource = self;
    sourceTab.delegate = self;
    sourceTab.backgroundColor = RGBColor(239, 239, 244);
    sourceTab.rowHeight = UITableViewAutomaticDimension;
    sourceTab.estimatedRowHeight = 80;
    [sourceTab registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    [sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([BillDetailCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([BillDetailCell class])];
    [self.view addSubview:sourceTab];
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    sourceTab.tableHeaderView = headView;
    sourceTab.tableFooterView = [[UIView alloc]init];
    self.sourceTab = sourceTab;
    sourceTab.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.curPage++;
        [weakSelf getWithDrawLsit];
    }];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 40;
    }
    return 70;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.sources.count+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.text = @"账户明细";
        return cell;
    }
    BillDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([BillDetailCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.sources[indexPath.row-1];
    return cell;
}

#pragma mark ---------------------账户详情------------------
- (void)getMyCount
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    [self showWaiter];
    [MyCountNetwork getMyCountWithParame:parame response:^(BOOL success, NSString *msg,MyCountModel *model) {
        [self hideWaiter];
        if (success) {
            [self hideNoDataView];
            self.model = model;
            self.header.model = model;
            self.sourceTab.hidden = NO;
        }else{
            if ([msg isEqualToString:NoNetworkStatus]) {
                [self showRequestAgainView:@selector(getMyCount)];
                self.sourceTab.hidden = YES;
            }
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (void)getWithDrawLsit
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"pageNo"] = @(self.curPage);
    [self showClearBgWaiter];
    [MyCountNetwork getWithDrawListWithParame:parame response:^(BOOL success, NSString *msg, NSArray *lists) {
        [self hideWaiter];
        if (success) {
            [self hideRequestAgainView];
            [self.sourceTab.mj_footer endRefreshing];
            self.sourceTab.mj_footer.hidden = lists.count<20;
            if (lists.count == 0&&self.curPage==1) {
                [self showNoDataLab:self.sourceTab range:CGRectMake(0, self.sourceTab.height*0.5, ScreenW, 20) title:@"暂无提现记录"];
                return;
            }
            [self.sources addObjectsFromArray:lists];
            [self.sourceTab reloadData];
        }else{
            if ([msg isEqualToString:NoNetworkStatus]) {
                [self showRequestAgainView:@selector(getWithDrawLsit)];
            }
            self.sourceTab.mj_footer.hidden = YES;
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (NSMutableArray *)sources
{
    if (!_sources) {
        _sources = [NSMutableArray array];
    }
    return _sources;
}

@end
